<template>
    <div>
        <!-- 顶部滑动条区域 -->
        <div id="slider" class="mui-slider ">
            <div id="sliderSegmentedControl" class="mui-scroll-wrapper mui-slider-indicator mui-segmented-control mui-segmented-control-inverted">
                <div class="mui-scroll">
                    <a :class="['mui-control-item' ,item.id===0?'mui-active':'']"
                    v-for="item in imgcategory" :key="item.id" @tap="getImages(item.id)">
                    <!-- mui组件内部，除了使用click还可以用tap -->
                        {{ item.title }}
                    </a>
                </div>
            </div>
        </div>

        <!-- 图片列表区域 -->
        <ul class="photolist">
          <router-link v-for="item in imglist" :key="item.id" :to="'/home/photoinfo/' + item.id"  tag="li">
            <img v-lazy="item.img_url">
            <div class="info">
                <h1 class="info-title">{{ item.title }}</h1>
                <div class="info-body">{{ item.zhaiyao }}</div>
            </div>
          </router-link>
        </ul>
    </div>
</template>

<script>
import mui from '../../lib/mui/js/mui.min.js'

export default{
    data(){
        return{
            imgcategory:[],//所有图片列表分类导航
            imglist:[]//图片数据
        }
    },
    created(){
        this.getImgCategory();
        //默认进入页面，请求 全部 图片
        this.getImages(0)
    },
    mounted(){
        //当组件中的DOM结构被渲染好并放在页面中，会执行这个钩子函数
        //如果要操作元素，最好在mounted中，因为这个时候的DOM元素是最新的
        //初始化顶部滑动条
        mui('.mui-scroll-wrapper').scroll({
            deceleration: 0.0005 //flick 减速系数，系数越大，滚动速度越慢，滚动距离越小，默认值0.0006
        });
    },
    methods:{
        getImgCategory(){//获取所有图片分类
            this.axios
            .get('api/getimgcategory')
            .then(result=>{
                if(result.data.status === 0){
                    // console.log(result.data.message);
                    //手动拼接出最完整的分类列表
                    result.data.message.unshift({title:"全部",id:0});
                    this.imgcategory = result.data.message;
                }

            })
        },
        getImages(id){//根据分类id获取图片
            this.axios
            .get('api/getimages/' + id)
            .then(result => {
                if(result.data.status === 0){
                    // console.log(result.data.message);
                    this.imglist = result.data.message;
                }
            })
        }
    }

}
</script>

<style lang="scss" scoped>
*{
    touch-action:pan-y;
}

.photolist{
    list-style: none;
    padding:10px 10px 0;
    margin:0;
    li{
        position: relative;
        background-color:#ccc;
        text-align:center;
        margin-bottom: 10px;

        img{
            // display:block;
            width:100%;
            vertical-align: middle;//去掉行内块元素的边距
            box-shadow:0 0 4px #999;
        }
        img[lazy=loading] {
            width: 40px;
            height: 300px;
            margin: auto;
        }

        .info{
            position: absolute;
            bottom:0;
            left:0;
            background-color: rgba(0,0,0,.5);
            max-height:94px;
            display: -webkit-box;
            -webkit-box-orient: vertical;
            -webkit-line-clamp: 3;
            overflow: hidden;
            color:#fff;
            text-align:left;

            .info-title{
                font-size:14px;
            }
            .info-body{
                font-size:12px;
                line-height:1.5;
            }
        }
    }
}


</style>